@using Masa.BuildingBlocks.StackSdks.Config;
@using Masa.Contrib.StackSdks.Config;
@inherits ProComponentBase
@inject IMasaStackConfig MasaStackConfig
@page "/job/task/{JobId}"

<PageTitle>@T("Task")</PageTitle>
<AutoHeight Overflow AutoClass="mt-6">
    <HeaderContent>
        <MRow Class="flex-none d-inline-flex pr-4" NoGutters>
            <MCol>
                <span class="emphasis--text h5">
                    @_job?.Name
                </span>
            </MCol>
            <MSpacer />
            <MCol Md="2">
                <SSelect Small @bind-Value="_queryTimeType"
                         Items="_jobQueryTimeTypeList"
                         Label="@T("QueryTimeType")"
                         ItemText="e => T(e==default?string.Empty:e.ToString())"
                         ItemValue="e=> e"
                         BackgroundColor="white"
                         Class="rounded-2 body2">
                </SSelect>
            </MCol>
            <SDateTimeRangeToolbar class="ml-6" ShowQuickChange="false" StartDateTime=_queryStartTime EndDateTime=_queryEndTime OnUpdate="QueryTimeChanged" Clearable></SDateTimeRangeToolbar>
            @if (IsApiCreate)
            {
                <MCol Md="2" Class="ml-6">
                    <SSelect Small Value="_queryOrigin" TItem="string" TItemValue="string" TValue="string" ValueChanged="QueryOriginChanged" Label="@T("Origin")" BackgroundColor="white" Class="rounded-2 body2" Clearable Items="_orginList" ItemText="origin => origin" ItemValue="origin=>origin">
                    </SSelect>
                </MCol>
            }
        </MRow>
        <MRadioGroup Value="_queryStatus" TValue="TaskRunStatus" ValueChanged="QueryStatusChanged" OnClick="RadioGroupClickHandler" Row Class="mt-0 pt-2 masa-radiogroup">
            <MRadio TValue="TaskRunStatus" Value="default" Color="green">
                <LabelContent>
                    <span>@T("All")</span>
                </LabelContent>
            </MRadio>
            @foreach (KeyValuePair<string, TaskRunStatus> pair in _queryStatusList)
            {
                <MRadio TValue="TaskRunStatus" Value="pair.Value" Color="green">
                    <LabelContent>
                        <span> @T(pair.Key) </span>
                    </LabelContent>
                </MRadio>
            }
        </MRadioGroup>
    </HeaderContent>
    <AutoHeightContent>
        <MCard Class="full-height pa-2" Style="border-radius: 16px !important;position:relative;">
            @if (_tasks.Any())
            {
                <SDataTable Headers="Headers" Items="_tasks" TItem="SchedulerTaskDto" ItemsPerPage="_pageSize" HideDefaultFooter Class="d-flex full-height flex-column masa-tasktable">
                    <HeaderColContent Context="header">
                        @if (header.Value != nameof(SchedulerTaskDto.Origin) || (header.Value == nameof(SchedulerTaskDto.Origin) && IsApiCreate))
                        {
                            <span class="text-btn">@header.Text</span>
                        }
                    </HeaderColContent>
                    <ItemColContent>
                        @switch (context.Header.Value)
                        {
                            case nameof(SchedulerTaskDto.SchedulerTime):
                                <span class="body2 regular--text">
                                    <DateTimeFormat Value="context.Item.SchedulerTime" Format="@(T("$DateTimeFormat"))">
                                    </DateTimeFormat>
                                </span>
                                break;
                            case (nameof(SchedulerTaskDto.TaskRunStartTime)):
                                <span class="body2 regular--text">
                                    <DateTimeFormat Value="context.Item.TaskRunStartTime" Format="@(T("$DateTimeFormat"))">
                                    </DateTimeFormat>
                                </span>
                                break;
                            case nameof(SchedulerTaskDto.TaskRunEndTime):
                                <span class="body2 regular--text">
                                    <DateTimeFormat Value="context.Item.TaskRunEndTime" Format="@(T("$DateTimeFormat"))">
                                    </DateTimeFormat>
                                </span>
                                break;
                            case (nameof(SchedulerTaskDto.TaskStatus)):
                                <span class="text-body4 neutral-lighten-2--text d-flex">
                                    <SButton Icon XSmall Style="cursor:default">
                                        <SIcon Color="@ComputedStatusColor(context.Item.TaskStatus)" IsDefaultToolTip="false">mdi-checkbox-blank-circle</SIcon>
                                    </SButton>
                                    <div style="padding-left:4px; color: @ComputedStatusColor(context.Item.TaskStatus)" class="body2">
                                        @(context.Item.TaskStatus == 0 ? @T("NotRunning") : @T(context.Item.TaskStatus.ToString()))
                                    </div>
                                </span>
                                break;
                            case (nameof(SchedulerTaskDto.RunTime)):
                                var runtime = GetRunTime(context.Item.TaskRunStartTime, context.Item.TaskRunEndTime);
                                <span class="body2 emphasis2--text">@runtime</span>
                                break;
                            case nameof(SchedulerTaskDto.RunCount):
                                <span class="body2 emphasis2--text">@context.Item.RunCount</span>
                                break;
                            case nameof(SchedulerTaskDto.Origin):
                                @if (IsApiCreate)
                                {
                                    <span class="body2 emphasis2--text">@context.Item.Origin</span>
                                }
                                break;
                            case nameof(SchedulerTaskDto.OperatorId):
                                <span class="body2 regular--text">@context.Item.OperatorName</span>
                                break;
                            case "Action":
                                <MTooltip Bottom>
                                    <ActivatorContent Context="tooltips">
                                        <MButton Icon @attributes="@tooltips.Attrs" Width="20" Height="20" OnClickStopPropagation OnClick="()=>OpenTaskLog(context.Item)">
                                            <SIcon>md:plagiarism</SIcon>
                                        </MButton>
                                    </ActivatorContent>
                                    <ChildContent>
                                        <span>@T("TaskLog")</span>
                                    </ChildContent>
                                </MTooltip>
                                @* @if (!string.IsNullOrEmpty(context.Item.TraceId)) *@
                                {
                                    <MTooltip Bottom>
                                        <ActivatorContent Context="tooltips">
                                            <MButton Icon @attributes="@tooltips.Attrs" Width="20" Height="20" OnClickStopPropagation Class="ml-6" OnClick="()=>OpenTaskTrace(context.Item)">
                                                <SIcon>mdi-chart-timeline-variant</SIcon>
                                            </MButton>
                                        </ActivatorContent>
                                        <ChildContent>
                                            <span>@T("TaskTrace")</span>
                                        </ChildContent>
                                    </MTooltip>
                                }
                                @if (@context.Item.TaskStatus == TaskRunStatus.WaitToRun)
                                {
                                    <SIcon Small Color="neutral-lighten-2" Class="ml-6" Size="20" OnClick="()=> OnShowConfirmDialog(ConfirmDialogTypes.StopTask,context.Item.Id)" Tooltip="@T("StopTask")">
                                        mdi-stop-circle-outline
                                    </SIcon>
                                }
                                else
                                {
                                    <MMenu Bottom Left Class="ml-2">
                                        <ActivatorContent Context="menuContext">
                                            <SButton Color="" Icon @attributes="@menuContext.Attrs" Width="20" Height="20" OnClickStopPropagation Class="ml-6">
                                                <SIcon>mdi-dots-vertical</SIcon>
                                            </SButton>
                                        </ActivatorContent>
                                        <ChildContent>
                                            <MList>
                                                <MListItem Link OnClick="()=> OnShowConfirmDialog(ConfirmDialogTypes.RestartTask,context.Item.Id)">
                                                    <MListItemIcon>
                                                        <SIcon>mdi-repeat</SIcon>
                                                    </MListItemIcon>
                                                    <MListItemContent>
                                                        <MListItemTitle>
                                                            @(T("Rerun"))
                                                        </MListItemTitle>
                                                    </MListItemContent>
                                                </MListItem>
                                                @if (context.Item.TaskStatus == TaskRunStatus.Running || context.Item.TaskStatus == TaskRunStatus.Timeout || context.Item.TaskStatus == TaskRunStatus.WaitToRetry || context.Item.TaskStatus == TaskRunStatus.WaitToRun)
                                                {
                                                    <MListItem Link OnClick="()=> OnShowConfirmDialog(ConfirmDialogTypes.StopTask,context.Item.Id)">
                                                        <MListItemIcon>
                                                            <SIcon>mdi-delete-outline</SIcon>
                                                        </MListItemIcon>
                                                        <MListItemContent>
                                                            <MListItemTitle>
                                                                @(T("StopTask"))
                                                            </MListItemTitle>
                                                        </MListItemContent>
                                                    </MListItem>
                                                }
                                            </MList>
                                        </ChildContent>
                                    </MMenu>
                                }
                                break;
                            default:
                                <span class="body regular--text">@context.Value</span>
                                break;
                        }
                    </ItemColContent>
                    <FooterContent>
                        <SPagination Class="mt-3 mx-6 pb-6" @bind-Page="Page" @bind-PageSize=PageSize Total="_total" />
                    </FooterContent>
                </SDataTable>
            }
            else
            {
                <SEmptyPlaceholder />
            }
        </MCard>
    </AutoHeightContent>
</AutoHeight>


<SSimpleDialog OkText="@T("Sure")" OnOk="OnSure" @bind-Value="_showConfirmDialog" Title="@_confirmTitle" HideCancel OkColor="#FF5252" OkClass="full-width scheduler-confirm-dialog-ok-button">
    <ChildContent>
        <span class="d-flex flex-column justify-center align-center" style="text-align:center">
            @_confirmMessage
        </span>
    </ChildContent>
</SSimpleDialog>

<SSimpleModal CancelText="@T("Close")" @bind-Value="_showTraceDialog" Title="@T("TaskTrace")" Class="max-width">
    <ChildContent>
        <Masa.Scheduler.Web.Admin.Pages.Tsc.TraceDetail TraceId="@_current!.TraceId" Start="_current.TaskRunStartTime.UtcDateTime.AddMinutes(-15)" End="_current.TaskRunStartTime.UtcDateTime.AddMinutes(15)" />
    </ChildContent>
</SSimpleModal>

<SSimpleModal CancelText="@T("Close")" @bind-Value="_showLogDialog" Title="@T("TaskLog")" Class="max-width">
    <ChildContent>
        <Masa.Scheduler.Web.Admin.Pages.Tsc.LogList TaskId="@_current!.Id.ToString()" Start="_current.TaskRunStartTime.UtcDateTime.AddMinutes(-15)" End="_current.TaskRunStartTime.UtcDateTime.AddMinutes(15)" />
    </ChildContent>
</SSimpleModal>